package com.ruoyi.article.service.impl;

import com.ruoyi.article.domain.Article;
import com.ruoyi.article.mapper.ArticleMapper;
import com.ruoyi.article.service.ArticleService;
import com.ruoyi.article.utils.ContentUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public void execute() {
        System.out.println("MyService.execute() is called!");
    }

    @Override
    public List<Article> queryArticles() {
        return articleMapper.queryArticles();
    }

    @Override
    public int addArticle(Article article) {
        return articleMapper.addArticle(article);
    }

    @Override
    public Article getArticleById(int articleId) {
        Article article = articleMapper.getArticleById(articleId);
        try {
            // 使用反射调用ContentUtils中的方法添加水印与敏感测替换
            Method addWatermarkMethod = ContentUtils.class.getDeclaredMethod("addWatermark", String.class);
            Method filterSensitiveWordsMethod = ContentUtils.class.getDeclaredMethod("filterSensitiveWords", String.class);
            String contentWithWatermark = (String) addWatermarkMethod.invoke(null, article.getContent());
            contentWithWatermark = (String) filterSensitiveWordsMethod.invoke(null, contentWithWatermark);
            article.setContent(contentWithWatermark);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return article;
    }

    @Override
    public int deleteArticleById(int articleId) {
        return articleMapper.deleteArticleById(articleId);
    }

}
